#include <gtest/gtest.h>
#include <vector>
#include <unordered_map>

using namespace ::std;

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int size = nums.size();
        unordered_map<int, int> m;
        for(int i = 0, dif = target - nums[0]; i < size; ++i, dif = target - nums[i]) { // 边存边找
            if(m.find(dif) != m.end()) return {m[dif], i};
            else m[nums[i]] = i;
        }
        return {};
    }
};

class TwoSumCheck : public ::testing::Test {
public:
	class Solution solution;
};

TEST_F(TwoSumCheck, testTwoSum_1) {
	vector<int> nums = {10, 2, 3, 4, 5, 6};
	EXPECT_TRUE((vector<int>{1, 2}) == (solution.twoSum(nums, 5)));
}

TEST_F(TwoSumCheck, testTwoSum_2) {
	vector<int> nums({7, 2, 11, 15});
	EXPECT_TRUE(vector<int>({0, 1}) == solution.twoSum(nums, 9));
}

TEST_F(TwoSumCheck, testTwoSum_3) {
	vector<int> nums({3, 3, 4});
	EXPECT_TRUE(vector<int>({0, 1}) == solution.twoSum(nums, 6));
}

TEST_F(TwoSumCheck, testTwoSum_4) {
	vector<int> nums({0, 3, -3, 4, -1});
	EXPECT_TRUE(vector<int>({0, 4}) == solution.twoSum(nums, -1));
}

int main() {
	::testing::InitGoogleTest();
	return RUN_ALL_TESTS();
}
